//
//  XLCollectionAdapter.m
//  campus
//
//  Created by xl on 2019/12/15.
//  Copyright © 2019 xl. All rights reserved.
//

#import "XLCollectionAdapter.h"

@interface XLCollectionAdapter ()

@property (nonatomic, strong)   NSMutableArray* adapterCollectionArray;

@end
@implementation XLCollectionAdapter

- (instancetype)init
{
    self = [super init];
    if (self) {
        _adapterCollectionArray = [NSMutableArray new];
    }
    return self;
}

- (NSMutableArray*)getAdapterArray
{
    return [_adapterCollectionArray copy];
}

- (void)setAdapterArray:(NSArray*)arr
{
    [_adapterCollectionArray removeAllObjects];
    [_adapterCollectionArray addObjectsFromArray:arr];
}

- (void)refreshCellByData:(id)data collectionView:(UICollectionView *)collectionView
{
    
}

#pragma mark    UICollectionVeiw DataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _adapterCollectionArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    id cellData = [self.adapterCollectionArray objectAtIndex:indexPath.row];
    
    UICollectionViewCell* cell = NULL;
    XLSuppressPerformSelectorLeakWarning(
                                         cell = [self performSelector:NSSelectorFromString([NSString stringWithFormat:@"collectionView:cellFor%@:", [cellData class]]) withObject:collectionView withObject:cellData];);
    
    return cell;
}

#pragma mark    UITableView Delegate

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    [collectionView deselectItemAtIndexPath:indexPath animated:false];
    
    id cellData = [self.adapterCollectionArray objectAtIndex:indexPath.row];
    if (self.adapterDelegate) {
        if ([_adapterDelegate respondsToSelector:@selector(didSelectCellData:)]) {
            [_adapterDelegate didSelectCellData:cellData];
        }
        if ([_adapterDelegate respondsToSelector:@selector(didSelectCellData:indexPath:)]) {
            [_adapterDelegate didSelectCellData:cellData indexPath:indexPath];
        }
    }
}


- (void)collectionView:(UICollectionView *)collectionView willDisplayCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.adapterDelegate) {
        if ([_adapterDelegate respondsToSelector:@selector(willDisplayCell:forRowAtIndexPath:)]) {
            [_adapterDelegate willDisplayCell:cell forRowAtIndexPath:indexPath];
        }
    }
}

@end
